{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6ac6efa6",
   "metadata": {},
   "source": [
    "\n",
    "* 学习人：郭善霖\n",
    "* 时间：2021-11-15\n",
    "* week12\n",
    "* 学习线上资源：[菜鸟教程]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07e5b73d",
   "metadata": {},
   "source": [
    "## 本周知识点\n",
    " \n",
    "### 1. 字典的一般方法\n",
    "\n",
    "> 1. 字典的查询\n",
    ">> 1. 先看字典的key 是否存在 ：in 查询成员是否存在的重要\n",
    ">> 2. 查询\n",
    "> 2. 字典的修改\n",
    "> 3. 字典的删除\n",
    "> 4. 字典的增加（dict_name[key] = value ）\n",
    "\n",
    "### 2 .字典的进阶方法\n",
    "\n",
    "> 1. 字典的初始化\n",
    ">> 2. fromkeys()\n",
    ">>> 3. setdefault()\n",
    "\n",
    "### 3. 字典的实践（一）课本\n",
    "\n",
    "### 4. 字典的实践（二）课本\n",
    "\n",
    "### 5. 字典的实践（三）课本"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "421885d6",
   "metadata": {},
   "source": [
    "## 1. 字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9482a6b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng', 'stu1803': 'Lihua'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"Xiaofeng\",\n",
    "    \"stu1803\":\"Lihua\"\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a189a56d",
   "metadata": {},
   "source": [
    "> 1. 字典的查找\n",
    ">> 2. 直接查找 dict_name[key] 当关键词key不存在时，报错： KeyError: 'stu1804'\n",
    "\n",
    ">> 2. get()方法 D.get(k[,d]) 当关键词存在的时候，返回None不存在。不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "921181ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接查找 dict_name[key]\n",
    "stu_info[\"stu1801\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "679a4f42",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1804'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-af87bde9363c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"stu1804\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'stu1804'"
     ]
    }
   ],
   "source": [
    "stu_info[\"stu1804\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aeaf053f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function get:\n",
      "\n",
      "get(key, default=None, /) method of builtins.dict instance\n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 用get方法查找\n",
    "help(stu_info.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "466e5e17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9542e67e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c1ef606",
   "metadata": {},
   "source": [
    "> 2. 修改 & 新增\n",
    ">> 1. 先查询 查看key书是否存在 使用 in 查询成员\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3f413a1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1804\n",
    "\"stu1804\" in stu_info\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ce93af5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1803\n",
    "\"stu1803\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b80aedad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果key存在 修改 \"stu1803\":\"Lihua\"  --> \"stu1803\":\"Liuyu\"\n",
    "if \"stu1803\" in stu_info:\n",
    "    stu_info[\"stu1803\"] = \"LiuYu\"\n",
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f232a863",
   "metadata": {},
   "source": [
    "> 3. 字典的删除\n",
    ">> 1. D.pop 的一般操作如下：\n",
    ">>> 1. 先查询是否存在 in\n",
    ">>> 2. 在使用删除的方法\n",
    ">> 2. D.popitem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "abea993b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b369e9f9",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-23-d1ada3d451b0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 不正确，不同于列表（删除最后一个）\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "stu_info.pop() # 不正确，不同于列表（删除最后一个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a4f47281",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典删除的正确使用\n",
    "if  'stu1802'in stu_info:\n",
    "    stu_info.pop( 'stu1802')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "00df4d36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ea012e58",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1804'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-9f21682da6f0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'stu1804'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'stu1804'"
     ]
    }
   ],
   "source": [
    "stu_info.pop('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4a689e06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "20ae876a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1803', 'LiuYu')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "129ba240",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "89f95030",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 1, 'u': 0}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 目标： 查询 元音字母是否出现在word文本当中，并且统计出现的次数\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for i in vowels:\n",
    "    # 使用字典的新增 构建初始化字典\n",
    "    found[i] = 0\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "# 循环用户输入的文本，出现一次元音字符，对应的元音字符+1\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "    \n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a5e8af5",
   "metadata": {},
   "source": [
    "> 字典的进阶方法：初始化所有值 fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d63633d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98aaaf56",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 初始化 字典\n",
    "found = found.fromkeys(vowels,0)\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4ebdbb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "> 字典的进阶方法：初始化特定的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e586e85",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
